﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Back.Xinkui.Entity.Advert;
using Back.Xinkui.WebModel;
using CommonFramework.Utility;
using Back.Xinkui.DataAccess.Advert;
using Back.Xinkui.Common.Enum;
using Xinkui.Common.Enum;
using Xinkui.Common;

public partial class advert_advertSpaceDetail : AdvertAspxBase
{
    protected AdvertSpaceEntity space = new AdvertSpaceEntity();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(WebHelper.GetFormValue<string>("SpaceName", "")))
        {
            InitData();
        }
        else
        {
            Action();
        }
    }

    private int id
    {
        get { return WebHelper.GetQueryStringValue<int>("ID", 0); }
    }
    private int boardId = 1;
    private string action
    {
        get { return WebHelper.GetQueryStringValue<string>("Action", string.Empty); }
    }
    private void InitData()
    {
        if (action == "add")
        {
            space.BoardID = boardId;
        }
        if (action == "edit")
        {
            space = AdvertDataAccess.GetAdvertSpaceByID(id);
        }
    }

    private void Action()
    {
        if (action == "edit")
        {
            AdvertSpaceEntity temp = AdvertDataAccess.GetAdvertSpaceByID(id);
            if (temp != null)
                space = temp;
        }
        space.BoardID = boardId;
        space.ID = id;
        space.SpaceName = WebHelper.GetFormValue<string>("SpaceName", "");
        space.SpaceType = WebHelper.GetFormValue<int>("SpaceType", (int)ESpecialKey.IntNoSearch);
        space.SourceType = WebHelper.GetFormValue<int>("SourceType", (int)ESpecialKey.IntNoSearch);
        space.Width = WebHelper.GetFormValue<int>("Width", 0);
        space.Height = WebHelper.GetFormValue<int>("Height", 0);
        space.MaxCount = WebHelper.GetFormValue<int>("MaxCount", 0);
        space.SpaceNo = WebHelper.GetFormValue<string>("SpaceNo", "");
        space.Description = WebHelper.GetFormValue<string>("Description", "");
        space.Url = WebHelper.GetFormValue<string>("Url", "");
        space.Code = WebHelper.GetFormHtmlValue<string>("Code", "");

        space.SizeType = Convert.ToInt16(WebHelper.GetFormValue<int>("SizeType", 0));
        space.BWidth = WebHelper.GetFormValue<int>("BWidth", 0);
        space.BHeight = WebHelper.GetFormValue<int>("BHeight", 0);

        if (FileUpload1.PostedFile.FileName.Length > 0)
        {
            FileEntity fileentity = Xinkui.Common.FileUpload.Upload(FileUpload1.PostedFile, ESourceType.SpacePic, 0, 0);
            if (!string.IsNullOrEmpty(fileentity.SavePath))
                space.ImageSrc = fileentity.SavePath;
        }
        if (string.IsNullOrEmpty(space.SpaceName) || space.SpaceType == (int)ESpecialKey.IntNoSearch || space.SourceType == (int)ESpecialKey.IntNoSearch
            || space.Width == 0 || space.Height == 0
            || string.IsNullOrEmpty(space.SpaceNo) || string.IsNullOrEmpty(space.Description) || string.IsNullOrEmpty(space.Code))
        {
            Show("请填写完整！");
            return;
        }

        if (action == "add")
        {
            AdvertDataAccess.CreateAdvertSpace(space);
            Show("添加成功！");
        }
        if (action == "edit")
        {
            AdvertDataAccess.UpdateAdvertSpace(space);
            Show("编辑成功！");
        }
    }
}